package xiaodou.com.testinterface;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.Request;
import xiaodou.com.testinterface.dialog.IPDialog;
import xiaodou.com.testinterface.net.Constance;
import xiaodou.com.testinterface.net.OkHttpManager;

public class MainActivity extends BaseActivity {

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addDialog();
    }

    /**
     * 设置IP dialog
     */
    private void addDialog() {
        Dialog dialog = new IPDialog(MainActivity.this,ipHandler);
        dialog.setTitle("IPDIALOG");
        dialog.show();
    }

    Handler ipHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            sp = getSharedPreferences("saveInfos", 0);
            if (msg.obj != null) {
                sp.edit().putString("ip", (String) msg.obj).commit();
            }
        };
    };

    /**
     * 服务启动状态
     * @param view
     */
    public void openService(View view){
        net(Constance.BASE_URL);
    }

    /**设置IP*/
    public void setIP(View view) {
        addDialog();
    }

    /**
     * 进门接口
     * 参数列表：phonenum 请求开门的用户手机号码
     * 备注:此接口可能会超时,请勿等待此接口返回结果
     * @param view
     */
    public void openDoor(View view){
        net(Constance.BASE_URL+"indoor?phonenum=18792157558");
    }

    /**
     *出门接口
     * 参数列表：phonenum 请求开门的用户手机号码,apiWeight 用户购买的商品重量
     * @param view
     */
    public void downDoor(View view){
        net(Constance.BASE_URL+"indoor?phonenum=18792157558&apiWeight=12.5");
    }

    /**
     * 强制出门接口
     * @param view
     */
    public void openAllDoor(View view){
        net(Constance.BASE_URL+"OpenAllDoor");
    }

    /**
     * 测试推送接口
     * 参数列表: style 枚举, indoor 测试推送进门设备 ,outdoor 测试推送出门设备
     * 备注:请以实际听到的声音为准，请勿相信返回的结果数据
     * @param view
     * 进门设备
     */
    public void socketIndoor(View view){
        net(Constance.BASE_URL+"socket?style=indoor");
    }

    /**
     * 出门设备
     * @param view
     */
    public void socketOutdoor(View view){
        net(Constance.BASE_URL+"socket?style=outdoor");
    }

    /**
     * 获得重量传感器数据接口
     * 备注:本接口可能由于重量传感器数据不稳定而无法获取数据造成超时,因此使用前请保持稳定
     * @param view
     */
    public void getAllWeight(View view){
        net(Constance.BASE_URL+"getAllWeight");
    }

    /**
     *店铺状态
     * 查看店铺服务状态接口
     * @param view
     */
    public void storeStatus(View view){
        net(Constance.BASE_URL+"status");
    }

    /**进行网络请求*/
    private void net(String url) {
        OkHttpManager.getInstance().getNet(url, new OkHttpManager.ResultCallback() {
            @Override
            public void onFailed(Request request, IOException e) {
                Toast.makeText(MainActivity.this,"网络异常："+e.getMessage(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onSuccess(String response) {
                Toast.makeText(MainActivity.this,"运行成功："+response.toString(),Toast.LENGTH_LONG).show();
            }
        });
    }


}

